#include "ContactPanel.h"

#include "Util.h"

#include "MessengerPanel.h"
#include "ContactTree.h"
#include "MainFrame.h"

#include "MsnMessenger.h"
#include "MsnDef.h"
#include "MsnContact.h"

enum{
	ID_MENU_STATUS_ONLINE = 2000,
	ID_MENU_STATUS_BUSY,
	ID_MENU_STATUS_IDLE,
	ID_MENU_STATUS_BE_RIGHT_BACK,
	ID_MENU_STATUS_AWAY,
	ID_MENU_STATUS_ON_THE_PHONE,
	ID_MENU_STATUS_OUT_FOR_LUNCH,
	ID_MENU_STATUS_HIDDEN
};

int ID_MENU_CHANGE_PICTURE = wxNewId();

int ID_MENU_OPTIONS = wxNewId();

int ID_MENU_SIGN_OUT = wxNewId();

int ID_BUTTON_CHANGE_IMAGE = wxNewId();

int ID_LINK_USERSTATUS = wxNewId();


int ID_BUTTON_ADD_CONTACT = wxNewId();

int ID_COMBOBOX_STATUS = wxNewId();

int ID_TEXTCTRL_SEARCH = wxNewId();

int ID_TREECTRL_CONTACT = wxNewId();
int ID_TREECTRL_SEARCH = wxNewId();

BEGIN_EVENT_TABLE(ContactPanel, wxPanel)

	
    
	EVT_TEXT(ID_TEXTCTRL_SEARCH, ContactPanel::OnSearchContact)

	EVT_HYPERLINK(ID_LINK_USERSTATUS, ContactPanel::OnUserLabelClicked) 

	EVT_MENU_RANGE(ID_MENU_STATUS_ONLINE, ID_MENU_STATUS_HIDDEN, ContactPanel::OnChangeStatus)

	EVT_MENU(ID_MENU_CHANGE_PICTURE, ContactPanel::OnChangeImage)
	EVT_MENU(ID_MENU_OPTIONS, ContactPanel::OnOptions)
	EVT_MENU(ID_MENU_SIGN_OUT, ContactPanel::OnSignOut)


END_EVENT_TABLE()

#include "../resource/xpm/page_close.xpm"

ContactPanel::ContactPanel(MessengerPanel* mp)
: wxPanel(mp->leftPanel, 5555, wxDefaultPosition, wxDefaultSize),messengerPanel(mp), statusMenu(0), userStatusIndex(0)
{

	wxString picPath = messengerPanel->messenger->GetDisplayPicture();

	wxBitmap picImage(picPath, wxBITMAP_TYPE_JPEG);
	userImage = new wxStaticBitmap(this, wxID_ANY, picImage, wxDefaultPosition, wxDefaultSize);
  
 //   userLabel = new wxButton(this, wxID_ANY, _T(""), wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER);
 //   wxFont font = wxFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
//    userLabel->SetFont(font);

	linkCtrl = new wxHyperlinkCtrl(this, ID_LINK_USERSTATUS, _T("test"), _T("url"), wxDefaultPosition, wxDefaultSize, wxHL_ALIGN_LEFT|wxNO_BORDER);
	wxFont font = linkCtrl->GetFont();
	font.SetWeight(wxFONTWEIGHT_BOLD);
	linkCtrl->SetFont(font);
	linkCtrl->SetNormalColour(*wxBLACK);

	//wxBitmap close(_T("delete.gif"), wxBITMAP_TYPE_GIF);
	//bitmapButton = new wxBitmapButton(this, wxID_ANY, close, wxDefaultPosition, wxDefaultSize);

 
	wxBoxSizer* rightSizer = new wxBoxSizer(wxVERTICAL);

	wxBoxSizer* imageSizer = new wxBoxSizer(wxHORIZONTAL);
	imageSizer->Add(userImage, 0, wxALL, 1);
    imageSizer->Add(rightSizer, 1, wxALL, 1);
	//imageSizer->Add(bitmapButton, 0, wxALL, 1);


//    rightSizer->Add(userLabel, 0, wxALL|wxALIGN_LEFT|wxEXPAND, 0);

	rightSizer->Add(linkCtrl, 0, wxALL|wxALIGN_LEFT|wxEXPAND, 1);


   	wxBoxSizer* searchSizer = new wxBoxSizer(wxHORIZONTAL);

	searchTextCtrl = new wxTextCtrl(this, ID_TEXTCTRL_SEARCH, wxEmptyString,wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER|wxWANTS_CHARS);
	
	searchTextCtrl->Connect(wxID_ANY, wxEVT_SET_FOCUS, (wxObjectEventFunction) (wxEventFunction) &ContactPanel::OnSearchSetFocus, NULL, this);
	searchTextCtrl->Connect(wxID_ANY, wxEVT_KILL_FOCUS, (wxObjectEventFunction) (wxEventFunction) &ContactPanel::OnSearchKillFocus, NULL, this);

	searchTextCtrl->SetDefaultStyle(wxTextAttr(wxNullColour, *wxLIGHT_GREY));
	searchTextCtrl->ChangeValue(_("Search Contact..."));

    searchSizer->Add(searchTextCtrl, 1, wxALL|wxEXPAND, 0);
 
    contactTreeCtrl = new ContactTreeCtrl(this, messengerPanel, ID_TREECTRL_CONTACT);
	searchTreeCtrl = new ContactTreeCtrl(this, messengerPanel, ID_TREECTRL_SEARCH);

    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(imageSizer, 0, wxTOP | wxBOTTOM | wxEXPAND, 5);
	sizer->Add(searchSizer, 0, wxALL | wxEXPAND, 0);
    sizer->Add(contactTreeCtrl, 1, wxEXPAND, 0);
	sizer->Add(searchTreeCtrl, 1, wxEXPAND, 0);

	searchTreeCtrl->Hide();

    SetSizer(sizer);

	UpdateUIText();

}

ContactPanel::~ContactPanel()
{
	DEBUG(_T("~ContactPanel"));


	wxDELETE(statusMenu);
}


void ContactPanel::UpdateUIText()
{
/*
	int selection = statusComboBox->GetSelection();

	statusComboBox->Clear();

	statusComboBox->Append(_("Online"));
	statusComboBox->Append(_("Busy"));
	statusComboBox->Append(_("Idle"));
	statusComboBox->Append(_("Be right back"));
	statusComboBox->Append(_("Away"));
	statusComboBox->Append(_("On the phone"));
	statusComboBox->Append(_("Out to lunch"));
	statusComboBox->Append(_("Hidden"));

	statusComboBox->SetSelection(selection);
*/

	linkCtrl->SetLabel(userName +_T(" (") + MsnDef::STATUS_DESC_ARRAY[userStatusIndex] + _T(")"));
	linkCtrl->Refresh();

	wxDELETE(statusMenu);

	statusMenu = new wxMenu();

	statusMenu->AppendRadioItem(ID_MENU_STATUS_ONLINE, _("Online"));
	statusMenu->AppendRadioItem(ID_MENU_STATUS_BUSY, _("Busy"));
	statusMenu->AppendRadioItem(ID_MENU_STATUS_IDLE, _("Idle"));
	statusMenu->AppendRadioItem(ID_MENU_STATUS_BE_RIGHT_BACK, _("Be right back"));
	statusMenu->AppendRadioItem(ID_MENU_STATUS_AWAY, _("Away"));
	statusMenu->AppendRadioItem(ID_MENU_STATUS_ON_THE_PHONE, _("On the phone"));
	statusMenu->AppendRadioItem(ID_MENU_STATUS_OUT_FOR_LUNCH, _("Out to lunch"));
	statusMenu->AppendRadioItem(ID_MENU_STATUS_HIDDEN, _("Hidden"));

	statusMenu->AppendSeparator();

    statusMenu->Append(ID_MENU_CHANGE_PICTURE, _("Change display picture"));
	statusMenu->Append(ID_MENU_OPTIONS, _("Options"));

	statusMenu->AppendSeparator();

	statusMenu->Append(ID_MENU_SIGN_OUT, _("Sign out"));


	statusMenu->Check(ID_MENU_STATUS_ONLINE + userStatusIndex, true);

	if(contactTreeCtrl->IsShown()){
		searchTextCtrl->ChangeValue(_("Search Contact..."));
	}
}

void ContactPanel::SetUserName(const wxString& name)
{
//    userLabel->SetLabel(name);
//    userLabel->Refresh();

	userName = name;

	linkCtrl->SetLabel(userName +_T(" (") + MsnDef::STATUS_DESC_ARRAY[userStatusIndex] + _T(")"));

	linkCtrl->Refresh();
}


void ContactPanel::SetUserStatus(const wxString& status)
{
	userStatusIndex = MsnDef::GetStatusIndex(status);

    wxString userStatusDesc = MsnDef::GetStatusDesc(status);
	
    //statusComboBox->SetSelection(index);

	linkCtrl->SetLabel(userName +_T(" (") + userStatusDesc + _T(")"));

	linkCtrl->Refresh();
}

void ContactPanel::OnChangeImage(wxCommandEvent& event)
{
	wxString defaultDir = _T("");
	wxFileDialog fileDialog(this, _("Select Image"), defaultDir, _T(""), _T("*.*"), wxFD_OPEN );
    if (fileDialog.ShowModal() == wxID_OK){
        wxString filePath = fileDialog.GetPath();
		wxImage image;
		if(image.LoadFile(filePath)){
			wxImage scaledImage = image.Scale(60, 60);
			userImage->SetBitmap(wxBitmap(scaledImage));

			messengerPanel->messenger->ChangeDisplayPicture(scaledImage);
		}
    }
}

void ContactPanel::OnUserLabelClicked(wxHyperlinkEvent& event)
{
	this->PopupMenu(statusMenu);
}

void ContactPanel::OnChangeStatus(wxCommandEvent& event)
{
	int id = event.GetId();
    int index = id - ID_MENU_STATUS_ONLINE;

	statusMenu->Check(id, true);

	messengerPanel->messenger->ChangeStatus( MsnDef::STATUS_CODE_ARRAY[index]);

	SetUserStatus(MsnDef::STATUS_CODE_ARRAY[index]);
}

void ContactPanel::OnOptions(wxCommandEvent& event)
{
	App::GetMainFrame()->OnOptions(event);
}

void ContactPanel::OnSignOut(wxCommandEvent& event)
{
	messengerPanel->SignOut();
}


void ContactPanel::OnSearchContact(wxCommandEvent& event)
{
	wxString value = searchTextCtrl->GetValue().Trim(true).Trim(false);

	if(!value.IsEmpty()){
		//display search result tree
		contactTreeCtrl->ShowMe(false);

		searchTreeCtrl->ShowMe(true);
		searchTreeCtrl->SearchContact(value);
	}else{
		//display contact tree
		searchTreeCtrl->Clear();
		searchTreeCtrl->ShowMe(false);

		contactTreeCtrl->ShowMe(true);	
	}

	this->GetSizer()->Layout();
}

void ContactPanel::OnSearchSetFocus(wxFocusEvent& event)
{
	if(contactTreeCtrl->IsShown()){
		searchTextCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, *wxWHITE));
		searchTextCtrl->ChangeValue(_T(""));
	}
}

void ContactPanel::OnSearchKillFocus(wxFocusEvent& event)
{
	if(contactTreeCtrl->IsShown()){
		searchTextCtrl->SetDefaultStyle(wxTextAttr(*wxLIGHT_GREY, *wxWHITE ));
		searchTextCtrl->ChangeValue(_("Search Contact..."));
	}
}

